package com.example.mydy.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.R;
import com.example.mydy.model.IncomeRecord;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class IncomeRecordAdapter extends RecyclerView.Adapter<IncomeRecordAdapter.ViewHolder> {
    private List<IncomeRecord> records = new ArrayList<>();
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());

    public void updateData(List<IncomeRecord> newRecords) {
        this.records = newRecords;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_income_record, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        IncomeRecord record = records.get(position);
        
        // 设置收益类型图标
        switch (record.getType()) {
            case "任务收益":
                holder.ivIncomeType.setImageResource(R.drawable.ic_task);
                break;
            case "悬赏收益":
                holder.ivIncomeType.setImageResource(R.drawable.ic_reward);
                break;
            case "下级收益":
                holder.ivIncomeType.setImageResource(R.drawable.ic_invite);
                break;
            default:
                holder.ivIncomeType.setImageResource(R.drawable.ic_income);
        }

        // 设置收益描述
        holder.tvIncomeDescription.setText(record.getDescription());
        
        // 设置时间
        holder.tvIncomeTime.setText(dateFormat.format(new Date(record.getTimestamp())));
        
        // 设置金额
        holder.tvIncomeAmount.setText(String.format("+%.2f", record.getAmount()));
    }

    @Override
    public int getItemCount() {
        return records.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivIncomeType;
        TextView tvIncomeDescription;
        TextView tvIncomeTime;
        TextView tvIncomeAmount;

        ViewHolder(View itemView) {
            super(itemView);
            ivIncomeType = itemView.findViewById(R.id.iv_income_type);
            tvIncomeDescription = itemView.findViewById(R.id.tv_income_description);
            tvIncomeTime = itemView.findViewById(R.id.tv_income_time);
            tvIncomeAmount = itemView.findViewById(R.id.tv_income_amount);
        }
    }
} 